package com.example.hxds.nebula.service.impl;

import cn.hutool.core.util.IdUtil;
import com.example.hxds.common.exception.HxdsException;
import com.example.hxds.nebula.db.dao.OrderMonitoringDao;
import com.example.hxds.nebula.db.dao.OrderVoiceTextDao;
import com.example.hxds.nebula.db.pojo.OrderVoiceTextEntity;
import com.example.hxds.nebula.service.MonitoringService;
import com.example.hxds.nebula.task.VoiceTextCheckTask;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Service
@Slf4j
public class MonitoringServiceImpl implements MonitoringService {

    @Resource
    private OrderVoiceTextDao orderVoiceTextDao;
    @Resource
    private OrderMonitoringDao orderMonitoringDao;

    @Value("${minio.endpoint}")
    private String endpoint;

    @Value("${minio.access-key}")
    private String accessKey;

    @Value("${minio.secret-key}")
    private String secretKey;
    @Autowired
    private VoiceTextCheckTask voiceTextCheckTask;

    @Override
    @Transactional
    //将录音上传 到MInio
    public void monitoring(MultipartFile file, String name, String text) {
        try {
            MinioClient minioClient = new MinioClient.Builder()
                    .endpoint(endpoint)
                    .credentials(accessKey, secretKey)
                    .build();
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket("hxds-record")
                    .object(name)
                    .stream(file.getInputStream(), -1, 20971520)
                    .contentType("audio/x-mpeg")
                    .build();
            minioClient.putObject(putObjectArgs);
        } catch (Exception e) {
            log.error("上传代驾录音文件失败", e);
            throw new HxdsException("上传代驾录音文件失败");
        }

        //文件名格式例如:2156356656617-1.mp3，我们要解析出订单号
        OrderVoiceTextEntity orderVoiceTextEntity = new OrderVoiceTextEntity();
        String[] temp = name.substring(0, name.indexOf(".mp3")).split("-");

        long orderId = Long.parseLong(temp[0]);
        String uuid = IdUtil.simpleUUID();
        orderVoiceTextEntity.setUuid(uuid);
        orderVoiceTextEntity.setOrderId(orderId);
        orderVoiceTextEntity.setRecordFile(name);
        orderVoiceTextEntity.setText(text);

        //把文稿保存到HBase
        int rows = orderVoiceTextDao.insert(orderVoiceTextEntity);

        if (rows != 1) {
            throw new HxdsException("保存录音文稿失败");
        }
        //执行文稿内容审查
        voiceTextCheckTask.checkText(orderId, text, uuid);
    }

    @Override
    public int insertOrderMonitoring(long orderId) {
        int rows = orderMonitoringDao.insert(orderId);
        if (rows != 1) {
            throw new HxdsException("添加订单监控摘要记录失败");
        }
        return rows;
    }
}